package ThreadDemo1;

import javax.sound.midi.SysexMessage;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 故我
 * Date: 2023-09-29
 * Time: 9:48
 */
public class ThreadDemo11 {
    public static void main(String[] args) {
        serial();
        parallel();
    }

    public static void serial() {
        long begin = System.currentTimeMillis();
        long a = 0;
        for (long i = 0; i < 100_0000_0000L; i++) {
            a++;
        }
        long b = 0;
        for (long i = 0; i < 100_0000_0000L; i++) {
            b++;
        }
        long end = System.currentTimeMillis();
        System.out.println("serial time is: " + (end - begin) + " ms");
    }

    public static void parallel() {
        Thread t1 = new Thread(() -> {
            long a = 0;
            for (long i = 0; i < 100_0000_0000L; i++) {
                a++;
            }
        });

        Thread t2 = new Thread(() -> {
            long b = 0;
            for (long i = 0; i < 100_0000_0000L; i++) {
                b++;
            }
        });
        long begin = System.currentTimeMillis();
        t1.start();
        t2.start();

        try {
            t1.join();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        try {
            t2.join();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        long end = System.currentTimeMillis();
        System.out.println("parallel time is: " + (end - begin) + " ms");
    }
}
